package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import  cn.stylefeng.guns.modular.yingbao.entity.UserBank;
import  cn.stylefeng.guns.modular.yingbao.mapper.UserBankMapper;
import  cn.stylefeng.guns.modular.yingbao.model.params.UserBankParam;
import  cn.stylefeng.guns.modular.yingbao.model.result.UserBankResult;
import   cn.stylefeng.guns.modular.yingbao.service.UserBankService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 开户行列表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-12
 */
@Service
public class UserBankServiceImpl extends ServiceImpl<UserBankMapper, UserBank> implements UserBankService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UserBankParam param){
        UserBank entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(UserBankParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserBankParam param){
        UserBank oldEntity = getOldEntity(param);
        UserBank newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserBankResult findBySpec(UserBankParam param){
        return null;
    }

    @Override
    public List<UserBankResult> findListBySpec(UserBankParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserBankParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryAll() {
        List<UserBank> userBanks = this.baseMapper.selectList(new QueryWrapper<UserBank>());
        if(userBanks.size() > 0){
            return ResponseData.success(userBanks);
        }
        return ResponseData.error(500,"暂无数据");
    }

    @Override
    public ResponseData queryById(String id) {
        UserBank userBank = this.baseMapper.selectById(id);
        if(userBank != null){
            return ResponseData.success(userBank);
        }
        return ResponseData.error(500,"未知错误");

    }

    private Serializable getKey(UserBankParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserBank getOldEntity(UserBankParam param) {
        return this.getById(getKey(param));
    }

    private UserBank getEntity(UserBankParam param) {
        UserBank entity = new UserBank();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
